﻿/**
* dataService - AngularJS module for web api management.
*
*
* Credits
* =======
*
* Copyright 2016 LonghronShen <LonghronShen@Hotmail.com>
*/

(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'ccp';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.service('dataService', ["$rootScope", "$http", "$q", "routeService", function ($rootScope, $http, $q, routeService) {
        var checkMessageCode = function (response) {
            return true;
        };

        this.sayHello = function (name) {
            // console.info("Calling " + name);
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url: '/api/Common/SayHello?name=' + name
            }).then(function successCallback(response) {
                deferred.resolve(response);
            }, function errorCallback(response) {
                deferred.reject({ message: "error!" });
            });
            return deferred.promise;
        };

        this.login = function (email, password) {
            // console.info("Calling " + "dataService.login...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Account.Login.Url), { Email: email, Password: password }, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.deleteAccount = function (userId) {
            // console.info("Calling " + "dataService.deleteAccount...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Account.DeleteAccount.Url, { userId: userId }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.setAccountLockoutState = function (userId, value) {
            // console.info("Calling " + "dataService.setAccountLockoutState...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.Tag.Url, { user_id: userId, disabled: value }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.logout = function (token) {
            // console.info("Calling " + "dataService.logout...");
            var deferred = $q.defer();
            if (!token || token == "") {
                deferred.reject({ message: "error!" });
            }
            $http.get(routeService.getUrl(routeService.Manage.Account.Logout.Url, { token: token }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getWebApiCredentials = function (startIndex, count) {
            // console.info("Calling " + "dataService.getWebApiCredentials...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.WebApiCredential.GetWebApiCredentials.Url, { startIndex: startIndex, count: count }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.addWebApiCredentials = function (appKey, appSecrect) {
            // console.info("Calling " + "dataService.addWebApiCredentials...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.WebApiCredential.AddWebApiCredentials.Url), { AppKey: appKey, AppSecrect: appSecrect }, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.removeWebApiCredential = function (id) {
            // console.info("Calling " + "dataService.removeWebApiCredential...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.WebApiCredential.RemoveWebApiCredential.Url, { id: id }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.getUsers = function (keyword, carCode, startTime, endTime, roleId, state, useCache, startIndex, count) {
            // console.info("Calling " + "dataService.getUsers...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.GetUsers.Url, { keyword: keyword, carCode: carCode, startTime: startTime.toISOString(), endTime: endTime.toISOString(), roleId: roleId, state: state, useCache: useCache, startIndex: startIndex, count: count }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getUserRoles = function () {
            // console.info("Calling " + "dataService.getUserRoles...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.roles.GetRoles.Url, {}), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.createUser = function (email, phoneNumber) {
            // console.info("Calling " + "dataService.createUser...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.CreateUser.Url, { email: email, phoneNumber: phoneNumber }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.getRealNameInfo = function (userId) {
            // console.info("Calling " + "dataService.getRealNameInfo...", userId);
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.User.GetRealNameInfo.Url, { user_id: userId }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserInfo = function (userInfo) {
            // console.info("Calling " + "dataService.addWebApiCredentials...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.User.PutUserInfo.Url), userInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserRealNameInfo = function (userRealnameInfo) {
            // console.info("Calling " + "dataService.putUserRealNameInfo...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.User.PutUserRealNameInfo.Url), userRealnameInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };

        this.putUserRealNameCertificationStatus = function ( userId , userRealnameCertificationInfo) {
            // console.info("Calling " + "dataService.putUserRealNameCertificationStatus...");
            var deferred = $q.defer();
            $http.put(routeService.getUrl(routeService.Manage.User.PutUserRealNameCertificationStatus.Url, { user_id: userId }), userRealnameCertificationInfo, {})
                .then(function successCallback(response) {
                    // console.info(response);
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.GetOrderList = function ( state, startingPlaceCode,  destinationCode,  carLength,  startIndex,  count) {
            // console.info("Calling " + "dataService.GetOrderList...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.Order.GetOrderList.Url, { state: state, startingPlaceCode: startingPlaceCode, destinationCode: destinationCode, carLength: carLength, startIndex: startIndex, count: count }), {})
                .then(function successCallback(response) {
                    console.info('测试',response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.GetMyOrderList = function (startIndex, count) {
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.Order.GetMyOrderList.Url, { startIndex: startIndex, count: count }), {})
                .then(function successCallback(response) {
                    console.info('测试', response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.AquireStorageSharedSigniture = function (containerName) {
            // console.info("Calling " + "dataService.GetOrderList...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.Storage.AquireStorageSharedSigniture.Url, { containerName: containerName}), {})
                .then(function successCallback(response) {
                    console.info('测试', response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.GetSuborderList = function (orderId) {
            // console.info("Calling " + "dataService.GetSuborderList...");
            var deferred = $q.defer();
            $http.get(routeService.getUrl(routeService.Manage.Order.GetSuborderList.Url, { orderId: orderId }), {})
                .then(function successCallback(response) {
                     //console.info('测试',response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.DeleteOrder = function (orderId) {
            // console.info("Calling " + "dataService.DeleteOrder...");
            var deferred = $q.defer();
            $http.post(routeService.getUrl(routeService.Manage.Order.DeleteOrder.Url, { orderId: orderId }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.reject({ message: "error!", response: response });
                    }
                    if (response.data.Error) {
                        deferred.reject({ message: "error!", response: response.data.Error.Message });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.reject({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.Area = function () {
            var deferred = $q.defer();
            $http.get(routeService.getUrlArea(routeService.Manage.Area.Area.Url), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.cityCode = function (citycode) {
            var deferred = $q.defer();
            $http.get(routeService.getUrlArea(routeService.Manage.Area.cityCode.Url, { citycode: citycode }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };
        this.provincecode = function (provincecode) {
            var deferred = $q.defer();
            $http.get(routeService.getUrlArea(routeService.Manage.Area.provincecode.Url, { provincecode: provincecode }), {})
                .then(function successCallback(response) {
                    // console.info(response);
                    if (!response.data) {
                        deferred.resolve({ message: "error!", response: response });
                    }
                    deferred.resolve(response);
                }, function errorCallback(response) {
                    deferred.resolve({ message: "error!", response: response });
                });
            return deferred.promise;
        };
    }]);
})();